package sealte.mynode.servlet.impl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sealte.mynode.bean.UserBean;
import sealte.mynode.servlet.system.SystemGetServlet;
import sealte.mynode.util.Tools;

public class UserLogout extends SystemGetServlet {

	private static final long serialVersionUID = 5386553798787007769L;

	@Override
	protected void doBusinessService(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		
		PrintWriter out = resp.getWriter();
		
		HttpSession ses = req.getSession();
		if(null==ses || null==ses.getAttribute("user")){
			out.println("You have been logout!");
			return ;
		}
		
		ses.invalidate();
		
		String idStr = req.getParameter("id");
		int id = Integer.parseInt(idStr);
		String date = Tools.getCurrentDate();
		UserBean ub = new UserBean();
		boolean flag = ub.logout(id, date);
		if(flag){
			resp.sendRedirect("index.jsp");
		}else{
			out.println("Logout failed!");
		}
		
		if(null!=out){
			out.close();
		}
	}

}
